/**********************************************************************
 *
 * Filename:    main.c  v0.1
 * 
 * Description: the main file.
 *
 * Notes:
 *
 **********************************************************************/

#include <stdio.h>
#include <string.h>
#include <getopt.h> 
#include <signal.h>
#include <stdlib.h>


#include "supplicant.h"
#include "log.h"


// all the configuratons:
char *client_name = NULL;  // cernet ..., hustcampus....
char *nic = NULL;
char *username = NULL;
char *password = NULL;
char *ip = NULL;
char *netmask = NULL;
char *netgate = NULL;
char *dns = "202.114.0.242";
char *broadcast_mac = "00-d0-f8-f9-9f-09";//"01-80-C2-00-00-03"
int echo_interval = 30;
int auto_reconnect = 0;
int auth_timeout = 3;
int server_timeout = 10;
int verbose = 0;
int stop_auth  = 0;


static char prog_version[] = "0.17";

static char prog_usage[] = "\n" \
	"\tUsage: xcat -ccernet -neth0 -uusername -ppassword -i192.168.0.1 -m255.255.255.0 -g192.168.0.254 -d202.114.0.242 -b00-d0-f8-f9-9f-09 -e30 -r -a3 -s10 -v0\n" \
	"\tUsage: xcat -h\n" \
	"\n";
 
static const char* short_options = "a:b:c:d:e:g:hi:m:n:p:rs:u:v";  
static struct option long_options[] = {
	{ "username", 		1,   NULL,    'u'},  
	{ "password", 		1,   NULL,    'p'},    
	{ "ip", 			1,   NULL,    'i'},
	{ "netmask", 		1,   NULL,    'm'},
	{ "netgate", 		1,   NULL,    'g'},
	{ "dns", 			1,   NULL,    'd'},
	{ "nic",    		1,   NULL,    'n'},
	{ "client_name",	1,   NULL,    'c'},
	{ "broadcast_mac",	1,   NULL,    'b'},  
	{ "echo_interval",	1,   NULL,    'e'},  
	{ "auto_reconnect",	0,   NULL,    'r'},  
	{ "auth_timeout", 	1,   NULL,    'a'},  
	{ "server_timeout",	1,   NULL,    's'},
	{ "help",	  		0,   NULL,    'h'},  
	{ "verbose",  		1,   NULL,    'v'},
	{      0,     		0,      0,      0} 
};  

static void usage(char *prog)
{
	fprintf(stderr,"Usage: %s %s %s", prog, prog_version, prog_usage);
}


static void sig_intr(int signo)
{
		stop_auth = 1;
}

int main(int argc, char *argv[])  
{
	supplicant_t *ps;

	int c; /* optarg, optind */ 
	while((c = getopt_long (argc, argv, short_options, long_options, NULL)) != -1){  
		switch (c){
		case 'a':
			auth_timeout = atoi(optarg);
			break;
		case 'b':
			broadcast_mac = optarg;
			break;
		case 'c':
			client_name = optarg;
			break;
		case 'd':
			dns = optarg;
			break;
		case 'e':
			echo_interval = atoi(optarg);
			break;
		case 'g':
			netgate = optarg;
			break;
		case 'h':
			usage(argv[0]);
			return 1;
		case 'i':  
			ip = optarg; 
			break;
		case 'm':
			netmask = optarg;
			break;
		case 'n':
			nic = optarg;
			break;
		case 'p':
			password = optarg;
			break;
		case 'r':
			auto_reconnect= 1;
			break;
		case 's':
			server_timeout= atoi(optarg);
			break;
		case 'u':
			username = optarg;
			break;
		case 'v':
			verbose = atoi(optarg);
			break;
		default:
			usage(argv[0]);
			//error info will be given automatically by getopt_long()... 
			return -1;
		}  
	}
	while(optind < argc){
		/* process 'argv[optind]' here */
		//fetches[fetches_cnt++] = argv[optind];
		optind++;
	}
	// Check the parameters:
	if(!client_name || !nic || !username || !password || !ip || !netmask || !netgate || !dns){
		fprintf(stderr, "ERROR: client, nic, username, ip, netmask, netgate, dns must be specified!\n");
		usage(argv[0]);
		return -1;
	}

	log_init(INFO);

	signal(SIGINT,sig_intr);  //We can exit with Ctrl+C

	ps=supplicant_create();
   	supplicant_show(ps);
	if(supplicant_run(ps)<0){
		goto error;
	}
	supplicant_destroy(ps);
   	return 0;
error:
	return -1;
} 


